//! Yaya安全服务层
//! 
//! 为Yaya libOS提供安全、可靠的服务管理框架

pub mod config;
pub mod mock_services;
pub mod desktop_environment;
pub mod shell_service;

pub use config::*;
pub use mock_services::*;
pub use desktop_environment::*;
pub use shell_service::*;

// 预导入模块
pub mod prelude {
    pub use crate::{
        ServiceManager,
        MemoryService,
        ProcessService,
        FileSystemService,
        NetworkService,
        GraphicsService,
        DesktopEnvironment,
        ShellService,
        ServiceError,
        ProcessState,
        ConnectionType,
        ConnectionState,
        FsNodeType,
        DisplayMode,
        WindowConfig,
        WindowType,
        SystemConfig,
        ServiceConfig,
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_memory_service() {
        let mem_service = MemoryService::new(std::sync::Arc::new(())).unwrap();
        let mut mem_guard = mem_service.lock().unwrap();
        
        // 测试内存分配
        let ptr = mem_guard.allocate(1024).unwrap();
        assert!(ptr > 0);
        
        // 测试内存写入和读取
        let test_data = vec![1, 2, 3, 4, 5];
        mem_guard.write_memory(ptr, &test_data).unwrap();
        let read_data = mem_guard.read_memory(ptr, test_data.len()).unwrap();
        assert_eq!(test_data, read_data);
        
        // 测试内存释放
        mem_guard.free(ptr).unwrap();
    }
    
    #[test]
    fn test_process_service() {
        let mem_service = MemoryService::new(std::sync::Arc::new(())).unwrap();
        let proc_service = ProcessService::new(std::sync::Arc::new(()), std::sync::Arc::new(std::sync::Mutex::new(mem_service))).unwrap();
        let mut proc_guard = proc_service.lock().unwrap();
        
        // 测试进程创建
        let pid = proc_guard.create_process("test_process", "echo 'test'", 4096).unwrap();
        assert!(pid > 0);
        
        // 测试进程信息获取
        let process_info = proc_guard.get_process_info(pid).unwrap();
        assert_eq!(process_info.pid, pid);
        assert_eq!(process_info.name, "test_process");
        
        // 测试进程列表
        let processes = proc_guard.list_processes();
        assert!(!processes.is_empty());
        
        // 测试进程终止
        proc_guard.terminate_process(pid).unwrap();
    }
    
    #[test]
    fn test_filesystem_service() {
        let fs_service = FileSystemService::new().unwrap();
        let mut fs_guard = fs_service.lock().unwrap();
        
        // 测试目录创建
        fs_guard.create_directory("/test_dir").unwrap();
        
        // 测试文件创建
        fs_guard.create_file("/test_dir/test.txt").unwrap();
        
        // 测试文件写入和读取
        let test_content = b"Hello, Yaya!";
        fs_guard.write_file("/test_dir/test.txt", test_content).unwrap();
        let read_content = fs_guard.read_file("/test_dir/test.txt").unwrap();
        assert_eq!(test_content, read_content.as_slice());
        
        // 测试目录列表
        let dir_contents = fs_guard.list_directory("/test_dir").unwrap();
        assert!(!dir_contents.is_empty());
        
        // 清理
        fs_guard.delete("/test_dir/test.txt").unwrap();
        fs_guard.delete("/test_dir").unwrap();
    }
    
    #[test]
    fn test_service_manager() {
        let service_manager = ServiceManager::new().unwrap();
        
        // 测试服务管理器启动
        service_manager.start_all_services().unwrap();
        assert!(service_manager.is_running());
        
        // 测试服务获取
        let mem_service = service_manager.get_memory_service();
        let proc_service = service_manager.get_process_service();
        let fs_service = service_manager.get_filesystem_service();
        let net_service = service_manager.get_network_service();
        let gfx_service = service_manager.get_graphics_service();
        let de_service = service_manager.get_desktop_environment();
        let shell_service = service_manager.get_shell_service();
        
        assert!(mem_service.lock().is_ok());
        assert!(proc_service.lock().is_ok());
        assert!(fs_service.lock().is_ok());
        assert!(net_service.lock().is_ok());
        assert!(gfx_service.lock().is_ok());
        assert!(de_service.lock().is_ok());
        assert!(shell_service.lock().is_ok());
        
        // 测试服务管理器停止
        service_manager.stop_all_services().unwrap();
        assert!(!service_manager.is_running());
    }
}